package com.example.dilidili_ting.view;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.activity.GerenxinxiActivityTing;
import com.example.dilidili_ting.activity.HistoryActivityTing;
import com.example.dilidili_ting.activity.SettingActivityTing;
import com.example.dilidili_ting.activity.UserInfoActivityTing;
import com.example.dilidili_ting.activity.loginActivityTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;

public class MyInfoViewTing {
    private RelativeLayout headTing;
    private TextView tvUsernameTing;
    private LinearLayout historyTing;
    private LinearLayout gerenxinxiTing;
    private LinearLayout shizhiTing;
    private Activity mContextTing;
    private LayoutInflater mInflaterTing;
    private View mCurrentViewTing;

    public MyInfoViewTing(Activity context){
        mContextTing = context;
        mInflaterTing = LayoutInflater.from(mContextTing);
    }


    private void initView() {
        mCurrentViewTing = mInflaterTing.inflate(R.layout.view_myinfo_ting,null);
        headTing = (RelativeLayout) mCurrentViewTing.findViewById(R.id.head_ting);
        tvUsernameTing = (TextView) mCurrentViewTing.findViewById(R.id.tv_username_ting);
        historyTing = (LinearLayout) mCurrentViewTing.findViewById(R.id.history_ting);
        gerenxinxiTing = (LinearLayout) mCurrentViewTing.findViewById(R.id.gerenxinxi_ting);
        shizhiTing = (LinearLayout) mCurrentViewTing.findViewById(R.id.shizhi_ting);
        loginStatus();
        headTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否已经登录
                if (SharedPreferencesUtilsTing.readLoginStatus(mContextTing)){
                    //todo:已登录跳转到个人资料界面
                    Intent intent=new Intent(mContextTing, UserInfoActivityTing.class);
                    mContextTing.startActivity(intent);
//                    mContextTing.overridePendingTransition(android.R.anim.slide_out_right,android.R.anim.slide_in_left);
                }else {
                    //todo:未登录跳转到登录界面
                    Intent intent=new Intent(mContextTing,  loginActivityTing.class);
                    mContextTing.startActivityForResult(intent,1);
                }
            }
        });

        historyTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SharedPreferencesUtilsTing.readLoginStatus(mContextTing)){
                    //todo:跳转到历史记录界面
                    Intent intent =new Intent(mContextTing, HistoryActivityTing.class);
                    mContextTing.startActivity(intent);
                }else {
                    new AestheticDialog.Builder(mContextTing, DialogStyle.TOASTER, DialogType.WARNING)
                            .setTitle("WARNING")
                            .setMessage("你还未登录，请先登录")
                            .setDuration(1500)
                            .show();
                }
            }
        });

        gerenxinxiTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SharedPreferencesUtilsTing.readLoginStatus(mContextTing)){
                    //todo：跳转到个人信息界面
                    Intent intent=new Intent(mContextTing, GerenxinxiActivityTing.class);
                    mContextTing.startActivityForResult(intent,1);

                }else {
                    new AestheticDialog.Builder(mContextTing, DialogStyle.TOASTER, DialogType.WARNING)
                            .setTitle("WARNING")
                            .setMessage("你还未登录，请先登录")
                            .setDuration(1500)
                            .show();
                }
            }
        });
        shizhiTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SharedPreferencesUtilsTing.readLoginStatus(mContextTing)){
                    //todo：跳转到设置界面，从设置页面回传参数
                    Intent intent=new Intent(mContextTing, SettingActivityTing.class);
                    mContextTing.startActivityForResult(intent,1);
                }else {
                    new AestheticDialog.Builder(mContextTing, DialogStyle.TOASTER, DialogType.WARNING)
                            .setTitle("WARNING")
                            .setMessage("你还未登录，请先登录")
                            .setDuration(1500)
                            .show();
                }
            }
        });
    }
    /**
     * 判断登录状态
     * */
    public void loginStatus(){
        if(SharedPreferencesUtilsTing.readLoginStatus(mContextTing)){
            tvUsernameTing.setText(SharedPreferencesUtilsTing.readLoginUserName(mContextTing));
        }else {
            tvUsernameTing.setText("请先登录");
        }
    }

    /**
     * 创建view界面
     * */
    private void createView(){
        initView();
    }
    /**
     * 获取当前view
     * */
    public View getView(){
        //判断当前view是否存在，不存在则创建view
        if (mCurrentViewTing == null){
            createView(); //创建view
        }
        return mCurrentViewTing; //返回当前view对象
    }
    /**
     * 显示当前view界面
     * */
    public void showView(){
        //
        if (mCurrentViewTing == null){
            createView();
        }
        mCurrentViewTing.setVisibility(View.VISIBLE);  //显示view
    }

}
